home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / isatty.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  1KB  |  42 lines

  1. /* from the original GCC TOS library by jrd */
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <fcntl.h>
  5. #include <ioctl.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <mintbind.h>
  9. #include "lib.h"
  10.  
  11. struct __open_file __open_stat[__NHANDLES];
  12.  
  13. int
  14. isatty(fd)
  15.   int fd;
  16. {
  17.   int rc;
  18.   long oldloc;
  19.   int handle = __OPEN_INDEX(fd);
  20.  
  21.   if (handle < __NHANDLES)
  22.     if (__open_stat[handle].status != FH_UNKNOWN)
  23.         return(__open_stat[handle].status == FH_ISATTY);
  24.   oldloc = Fseek(0L, fd, SEEK_CUR);    /* save current location */
  25.   if (Fseek(1L, fd, SEEK_CUR) != 0) {    /* try to seek ahead one byte */
  26.     /* got either a file position or an error (usually EBADARG indicating
  27.        a range error from trying to seek past EOF), so it is not a tty */
  28.     rc = 0;
  29.     (void) Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  30.   }
  31.   else
  32.     rc = 1;                /* yes, tty */
  33.   if (handle < __NHANDLES)
  34.     if (rc) {
  35.         __open_stat[handle].status = FH_ISATTY;
  36.         __open_stat[handle].flags = CRMOD|ECHO;
  37.     }
  38.     else
  39.         __open_stat[handle].status = FH_ISAFILE;
  40.   return (rc);            /* return true, false, or error */
  41. }
  42.